package com.yxy.springcloud.framework.unionauth.controller;

import com.yxy.springcloud.framework.common.base.BusinessException;
import com.yxy.springcloud.framework.common.base.RestResponse;
import com.yxy.springcloud.framework.unionauth.DTO.*;
import com.yxy.springcloud.framework.unionauth.service.TenantService;
import com.yxy.springcloud.framework.unionauth.service.UserService;
import com.yxy.springcloud.framework.unionauth.utils.CheckAppPermission;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author yuxiaoyang
 * 登录用户 admin_user相关接口
 */
@RestController
@RequestMapping("/app-user")
public class AdminUserController {
  @Resource
  private UserService functionService;

  @PostMapping("/list")
  public RestResponse getList(@RequestBody UserListDto query) {
    RestResponse restResponse = new RestResponse(200, "success");
    restResponse.setContent(functionService.getList(query));
    return restResponse;
  }

  @PostMapping("/createOrUpdate")
  public RestResponse createOrUpdate(@RequestBody AppUserCreateDto inputDto) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "success");
    restResponse.setContent(functionService.createOrUpdate(inputDto));
    return restResponse;
  }

  /**
   * 给某个app的前台vue页面直接调用,用于创建或修改app的登录用户;
   * 这样app user无需登录union auth的界面来创建其他的登录用户,提高用户满意度
   * @param inputDto
   * @return
   * @throws BusinessException
   */
  @CheckAppPermission
  @PostMapping("/createOrUpdate2")
  public RestResponse createOrUpdate2(@RequestBody AppUserCreateDto2 inputDto) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "success");
    restResponse.setContent(functionService.createOrUpdate2(inputDto));
    return restResponse;
  }

  @DeleteMapping("/{id}")
  public RestResponse delete(@PathVariable("id") long id) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "成功");
    functionService.delete(id);
    return restResponse;
  }

  /**
   * 从app后端发起的删除 app user的接口
   * @param id
   * @return
   * @throws BusinessException
   */
  @CheckAppPermission
  @GetMapping("/deleteFromApp")
  public RestResponse deleteFromApp(@RequestParam("id") long id) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "成功");
    functionService.delete(id);
    return restResponse;
  }

  /**
   * 超级管理员直接修改app登录用户密码,无需输入老密码 for 外部系统调用
   * 给外部系统调用时, 外部系统可能不知道user的id, 只能传递app code以及username, 我们将帮其定位到具体的user id
   * @param inputDto
   * @return
   * @throws BusinessException
   */
  @PostMapping("/resetPasswordForOuter")
  public RestResponse resetPasswordForOuter(@RequestBody ChangePasswordForOuterDto inputDto) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "密码修改成功");
    functionService.resetPasswordForOuter(inputDto);
    return restResponse;
  }

  /**
   * 超级管理员直接修改app登录用户密码,无需输入老密码 for union auth页面调用
   * @param inputDto
   * @return
   * @throws BusinessException
   */
  @PostMapping("/resetPasswordForInner")
  public RestResponse resetPasswordForInner(@RequestBody ChangePasswordForInnerDto inputDto) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "密码修改成功");
    functionService.resetPasswordForInner(inputDto.getUserId(), inputDto.getNewPassword());
    return restResponse;
  }

}
